package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.Class;
import com.campsys.entity.Course;
import com.campsys.entity.Teacher;
import com.campsys.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/class"})
public class ClassController {
    @Autowired
    private ClassService classService;
    @PostMapping("/list")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<Class> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Class> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) param.get("name");
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(Class::getName,name);
        }
        IPage result =classService.page(page,lambdaQueryWrapper);
        return Result.suc(result.getTotal(),result.getRecords());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Class class1){
        return classService.save(class1)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Class class1){
        return classService.updateById(class1)?Result.suc():Result.fail();
    }
    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return classService.removeById(id)?Result.suc():Result.fail();
    }

    @GetMapping("/getClass")
    public List<Class> getClasses(){
        return classService.getClasses();
    }

}
